import Vue from 'vue'
import Vuex from 'vuex'
import { login } from '../api/login'

Vue.use(Vuex)

export default new Vuex.Store({
  strict: true,
  state: {
    token: localStorage.getItem('token') || '',
    refresh_token: localStorage.getItem('refresh_token') || '',
    user: {
      photo: localStorage.getItem('photo') || ''
    }
  },
  mutations: {
    setToken (state, tokenStr) {
      state.token = tokenStr
      localStorage.setItem('token', tokenStr)
    },
    setRefreshToken (state, refreshTokenStr) {
      state.refresh_token = refreshTokenStr
      localStorage.setItem('refresh_token', refreshTokenStr)
    },
    setUser (state, a) {
      state.user = a
      localStorage.setItem('photo', a.photo)
    },
    setPhoto (state, a) {
      state.user.photo = a
      localStorage.setItem('photo', a.photo)
    },
    setName (state, a) {
      state.user.name = a
    }

  },
  actions: {
    async AloginAction (store, v) {
      console.log(1)
      const resp = await login(v)
      console.log(resp)
      const { token, refresh_token: refreshToken } = resp.data
      store.commit('setToken', token)
      store.commit('setRefreshToken', refreshToken)
    }
  },
  modules: {
  }
})
